public class Sqrtx {
    // x的平方根 https://leetcode.cn/problems/sqrtx/description/
    public int mySqrt(int x) {
        if(x<1) return 0;// x<1时,直接返回0
        long left=1,right=x;
        while(left<right) {
            long mid=left+(right-left+1)/2;// 防止数据过大时相乘会溢出
            if(mid*mid<=x) {
                left=mid;
            } else {
                right=mid-1;
            }
        }
        return (int)left;
    }
}
